home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news!enno
- From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
- Newsgroups: comp.lang.c++
- Subject: Re: Returning ref to object - is this code invalid?
- Date: 14 Jan 1996 19:43:27 GMT
- Organization: Fachbereich Informatik, TH Darmstadt
- Distribution: world
- Message-ID: <ENNO.96Jan14204327@kitz.inferenzsysteme.informatik.th-darmstadt.de>
- References: <30F937D8.12D2@sierra.net>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- In-reply-to: TGColwell's message of Sun, 14 Jan 1996 09:05:28 -0800
-
- In article <30F937D8.12D2@sierra.net> TGColwell <snowbull@sierra.net> writes:
-
- Does the following code create a dangling reference?
-
- class vector
- { private: float x,y,z;
- public: //c'tors & functions
- };
-
- class face
- { private: vector v1, v2, v3;
- public:
- vector& get_vtx_1() const;
- }
-
- vector& face::get_vtx_1() const //returns reference to vector
- { return v1;
- }
-
- void main()
- {
- face ff('constructor parameters);
- vector vv = ff.get_vtx_1();
- }
-
- I'm wondering if the return of a reference to v1 will invalidate
- my program since v1 goes out of scope when get_vtx_1() returns,
- or does v1 stay in scope since ff is not deleted?
-
- Any input is appreciated!
-
- 'v1' is a data-member of 'face' so it gets destroyed when the appropriate
- 'face' instances goes out of scope. Thus the program doesn't result in a
- dangling reference. However 'get_vtx_1' should return a const reference
- instead.
-
- Enno
-